// pages/send/send.js
Page({

  /**
   * Page initial data
   */
  data: {
    activity: "",
    name: "",

    host: "",

    time: "",

    place: "",

    comment: "",
    _id: "",
    btn: 0

  },

  onActivityChange: function(e) {
    this.setData({
      activity: e.detail.value
    })
  },

  onNameChange: function(e) {
    this.setData({
      name: e.detail.value
    })
  },

  onHostChange: function(e) {
    this.setData({
      host: e.detail.value
    })
  },

  onTimeChange: function(e) {
    this.setData({
      time: e.detail.value
    })
  },

  onPlaceChange: function(e) {
    this.setData({
      place: e.detail.value
    })
  },

  onCommentChange: function(e) {
    this.setData({
      comment: e.detail.value
    })
  },

  handleClick: function(e) {
    var that = this
    var data = this.data
    if (data.activity != "" &&
      data.name != "" &&
      data.host != "" &&
      data.time != "" &&
      data.place != "") {
      wx.showLoading({
        title: '发布中',
      })
      that.setData({
        btn: 1
      })
      //发布消息
      wx.request({
        url: 'https://www.ourspark.org/helloworld/publish',
        data: {
          _id: data._id,
          activity: data.activity,
          name: data.name,
          host: data.host,
          time: data.time,
          place: data.place,
          comment: data.comment
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function(res) {
          console.log(res)
          if (res.data.error_no == 2) {
            wx.showModal({
              title: "哎呀~",
              content: res.data.error_msg,
            })
          } else if (res.data.error_no == 1) {
            wx.hideLoading()
            that.setData({
              btn: 0
            })

            that.setData({
              activity: "",
              name: "",

              host: "",

              time: "",

              place: "",

              comment: ""
            })

            wx.showToast({
              title: '发布成功',
              icon: 'success',
              duration: 1000
            });
          }
        },
        fail: function(res) {
          wx.hideLoading()
          that.setData({
            btn: 0
          })

          wx.showToast({
            title: '发布失败',
            icon: 'none',
            duration: 1000
          });
        }
      })
    } else {
      wx.showModal({
        title: '提示~',
        content: '除备注外，其他选项不能为空哦！',
      })
    }
  },
  /**
   * Lifecycle function--Called when page load
   */
  onLoad: function(options) {
    var that = this
    this.setData({
      _id: options._id
    })
  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady: function() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow: function() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh: function() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom: function() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function() {

  }
})